/**
 * Created by koudaiwang on 2017/6/16.
 */

var config = require('config-lite');
var Mongolass = require('mongolass');
var mongolass = new Mongolass();
var moment = require('moment');
var objectIdToTimestamp = require('objectid-to-timestamp');
mongolass.connect(config.mongodb);

// 根据 id 生成创建时间 created_at
mongolass.plugin('addCreatedAt', {
    afterFind: function (results) {
        results.forEach(function (item) {
            item.created_at = moment(objectIdToTimestamp(item._id)).format('YYYY-MM-DD HH:mm');
        });
        return results;
    },
    afterFindOne: function (result) {
        if (result) {
            result.created_at = moment(objectIdToTimestamp(result._id)).format('YYYY-MM-DD HH:mm');
        }
        return result;
    }
});

// 管理员

exports.Admin = mongolass.model('Admin', {
    username: {type: 'string'},
    passwords: {type: 'string'},
    avatar: {type: 'string'},
    webLogin: {type: 'string'},
    webLoginTime: {type: 'string'},
    iOSLogin: {type: 'string'},
    iOSLoginTime: {type: 'string'},
    androidLogin: {type: 'string'},
    androidLoginTime: {type: 'string'},
    level: {type: 'number'} // 0 owner 1 admin 2 nomal admin
});
exports.Admin.index({username: 1, _id: -1}, { unique: true}).exec();

// ---------------------    用户相关       ----------------------------

exports.User = mongolass.model('User', {
    username: { type: 'string' },                                       // 用户名 用手机号注册
    passwords: { type: 'string' },                                      // 密码

    nickname: { type: 'string', default: ''},                            // 昵称
    avatar: { type: 'string', default: ''},                              // 头像
    gender: { type: 'string', enum: ['m', 'f', 'x'], default: 'm'},      // 性别
    bio: {type: 'string', default: ''},                                  // 个人简介
    score: {type: 'number', default: 0},                                 // 用户积分

    webLogin: {type: 'string', default: ''},
    webLoginTime: {type: 'string', default: ''},
    iOSLogin: {type: 'string', default: ''},
    iOSLoginTime: {type: 'string', default: ''},
    androidLogin: {type: 'string', default: ''},
    androidLoginTime: {type: 'string', default: ''},
});
exports.User.index({ username: 1 , _id: -1}, { unique: true }).exec();   // 根据用户名找到用户，用户名全局唯一

// ---------------------    新闻相关       ----------------------------

exports.News = mongolass.model('News', {
    author: {type: 'string'},
    title: {type: 'string'},
    content: {type: 'string'},
    type: {type: 'number', default: 0}, // 默认为 0 不分类  1 BTC 2 ETH 3 ...
    pv: {type: 'number'}
});
exports.News.index({ author: 1, _id: -1}).exec();

// ---------------------    获取新闻类型       ----------------------------

exports.NewsType = mongolass.model('NewsType', {
    type: {type: 'number'},
    typeName: {type: 'string'}

});
exports.NewsType.index({ type: 1, _id: -1}, {unique: true}).exec();



exports.CoinsDes = mongolass.model('CoinsDes', {
    coinname: {type: 'string'},
    coinimage: {type: 'string'},
    coindes: {type: 'string'},
    coinvalue: {type: 'number'},
});
exports.CoinsDes.index({ coinname: 1, _id: -1}, {unique: true}).exec();


exports.Tradingplatform = mongolass.model('Tradingplatform', {
    name: {type: 'string'},                 // 交易平台名称
    iconimage: {type: 'string'},            // 平台LOGO
    platformurl: {type: 'string'},          // 平台官网
    tradingVolume: {type: 'number'},        // 交易量
    foundDate: {type: 'string'},            // 创办日期
    des: {type: 'string'},                  // 介绍 平台官网及评级
    level: {type: 'number'},                // 平台评级
});
exports.CoinsDes.index({ name: 1, _id: -1}, {unique: true}).exec();


exports.ICOInfo = mongolass.model('ICOInfo', {
    name: {type: 'string'},                 // ICO 名称
    iconimage: {type: 'string'},            // ICO LOGO
    platformurl: {type: 'string'},          // 平台官网
    ICOPrice: {type: 'number'},             // ICO 价格
    nowPrice: {type: 'number'},             // 现在价格
    icodate: {type: 'string'},              // ICO时间
    icodes: {type: 'string'},               // ICO介绍
    cointotalnum: {type: 'number'},         // 币总数
});
exports.ICOInfo.index({ name: 1, _id: -1}, {unique: true}).exec();








